package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.service.IUserLoginService;
import cn.wolfcode.shop.vo.JSONResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {

    @Reference
    private IUserLoginService userLoginService;

    /**
     * 用户注册接口
     * @return 返回注册结果信息
     */
    @PostMapping
    public JSONResult userRegist(String username,String password){
        JSONResult jsonResult=new JSONResult();
        userLoginService.userRegist(username,password);
        return jsonResult;
    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("tokens")
    public JSONResult userLogin(String username,String password){
        JSONResult jsonResult=new JSONResult();
        String token=userLoginService.userLogin(username,password);
        Map map = new HashMap<>();
        map.put("token",token);
        jsonResult.setResult(map);
        return jsonResult;
    }

    /**
     * 注销
     * @param request 获取请求头信息：token
     * @return
     */
    @DeleteMapping("tokens")
    public JSONResult logout(HttpServletRequest request){
        JSONResult jsonResult=new JSONResult();
        userLoginService.logout(request.getHeader("token"));
        return jsonResult;
    }
}
